package com.qianfeng.wuyang.himalaya.adapter.recommendadapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qianfeng.wuyang.himalaya.BuildConfig;
import com.qianfeng.wuyang.himalaya.R;
import com.qianfeng.wuyang.himalaya.activity.MainActivity;
import com.qianfeng.wuyang.himalaya.bean.recommendbean.DiscoveryInfoEntity;
import com.qianfeng.wuyang.himalaya.bean.recommendbean.FocusImgInfoEntity;
import com.qianfeng.wuyang.himalaya.bean.recommendbean.GuesseEntity;
import com.qianfeng.wuyang.himalaya.bean.recommendbean.LikeEntity;
import com.qianfeng.wuyang.himalaya.bean.recommendbean.ListenInfoEntity;
import com.qianfeng.wuyang.himalaya.bean.recommendbean.ListenerEntity;
import com.qianfeng.wuyang.himalaya.bean.recommendbean.QualityEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by WangQing on 2016/12/8.
 */

public class FragmentRecommendAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements ViewPager.OnPageChangeListener {
    private final int HOTCODE=1; //热门推荐 听系列  小编推荐
    private final int GUESSCODE = 2;//猜你喜欢
    private final int FOCUSCODE = 3;//焦点图
    private final int DISCOVERYCODE=4;//发现 今日最火之类
    private final int QUALITYCODE=5;//精品听单
    private final int MORECODE=6;  //更多
    private final int FOOTCODE=7; //尾部广告轮播
    private final int RECOMMENDCODE=8;
    private final int HEADER = 9;

    private MainActivity activity;

    private Context context;
    private RecyclerView recyclerView;



    //discovery第二个item
    private List<DiscoveryInfoEntity> discoveryList=new ArrayList<>();

    //猜你喜欢
    private List<GuesseEntity> guessList = new ArrayList<>();
    private List<LikeEntity> likeList = new ArrayList<>();

    //热门推荐
    private List<ListenerEntity> list;
    private List<ListenInfoEntity> listenList = new ArrayList<>();

    //小编推荐
    private List<ListenInfoEntity> recommendList = new ArrayList<>();

    //精品听单
    private List<QualityEntity> qualityList = new ArrayList<>();

    //焦点图
    private List<View> focusviewList=new ArrayList<>();
    private List<FocusImgInfoEntity> focusList = new ArrayList<>();
    private  FocusesViewHolder focusesViewHolder;
//    private List<View> tagImg = new ArrayList<>();//保存第一个图片以及最后一个图片
    private Timer timer;
    //记录焦点图当前的页码
    private int focuscurposition=0;
    //handler  焦点图的轮播
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    focuscurposition++;
                    focusesViewHolder.focuseViewPager.setCurrentItem(focuscurposition,true);
            }
        }
    };

    public FragmentRecommendAdapter( Context context,MainActivity activity) {
        this.context = context;
        list = new ArrayList<>();
        this.activity = activity;
        this.activity=activity;
    }

    //猜你喜欢
    public void addLike(List<GuesseEntity> guesseEntitiesList,List<LikeEntity> likeList ){
        this.guessList.addAll(guesseEntitiesList);
        this.likeList.addAll(likeList);

        notifyDataSetChanged();
    }

    //小编推荐
    public void addRecommend(List<ListenInfoEntity> recommendList){
        this.recommendList.addAll(recommendList);
        notifyDataSetChanged();
    }

    //焦点图
    public void addFocusData(List<View> focusviewList,List<FocusImgInfoEntity> focusList){
        this.focusviewList.addAll(focusviewList);
        this.focusList.addAll(focusList);
        notifyDataSetChanged();
    }

    public  void addData(List<ListenerEntity> list){
        this.list.addAll(list);
//        listenList.addAll(listenInfoList);
        notifyDataSetChanged();
    }

    public void addDiscovery(List<DiscoveryInfoEntity> discoveryList){
//        if (this.discoveryList==null) {
            this.discoveryList.addAll(discoveryList);
            notifyDataSetChanged();
//        }
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.recyclerView = recyclerView;

//        LinearLayoutManager manager = new LinearLayoutManager(context);
//        manager.setOrientation(LinearLayoutManager.VERTICAL);
//        this.recyclerView.setLayoutManager(manager);

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType==FOCUSCODE){
            View rootView = LayoutInflater.from(context).inflate(R.layout.fragment_recommend_focusimgpager,parent,false);
            return new FocusesViewHolder(rootView);
        }else if (viewType==QUALITYCODE){
            View rootView = LayoutInflater.from(context).inflate(R.layout.fragment_recommend_quality,parent,false);
            return new QualityViewHolder(rootView);
        }
        else if (viewType==GUESSCODE){
            View rootView = LayoutInflater.from(context).inflate(R.layout.fragment_recommend_guesslike,parent,false);
            return new GuessViewHolder(rootView);
        }else if (viewType==DISCOVERYCODE){
            View view = LayoutInflater.from(context).inflate(R.layout.fragment_recommend_discovery,parent,false);
            return new DiscoveryViewHolder(view);
        }
            else if(viewType==MORECODE){
            View view = LayoutInflater.from(context).inflate(R.layout.fragment_recommend_more,parent,false);
            return new MoreViewHolder(view);
        }
        else if (viewType==FOOTCODE){

            if (BuildConfig.DEBUG) {
                Log.d("1620","oncreateview");
            }

            View view = LayoutInflater.from(context).inflate(R.layout.fragment_recommend_foot,parent,false);
            return  new FootViewHolder(view);
        }
        else if (viewType==RECOMMENDCODE){
            View view = LayoutInflater.from(context).inflate(R.layout.fragment_recommend_recommend,parent,false);
            return new RecommendViewHolder(view);
        }
        else {
            View rootView = LayoutInflater.from(context).inflate(R.layout.fragment_recommend_listen,parent,false);
            return  new HotViewHolder(rootView);
        }

    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case DISCOVERYCODE:
                bindDiscoveryViewHolder(holder,position);
                break;
            case HOTCODE:
                bindHotViewHolder(holder,position);
                break;
            case GUESSCODE:
                bindGuessViewHolder(holder,position);
                break;

            case RECOMMENDCODE:
                bindRecommendViewHolder(holder,position);
                break;
            case QUALITYCODE:
                bindQualityViewHolder(holder,position);
                break;
            case FOOTCODE:
                if (BuildConfig.DEBUG) {
                    Log.d("1620","onbind foot");
                }
                bindFootViewHolder(holder,position);
                break;
            case MORECODE:
                bindMoreViewHolder(holder,position);
                break;

            case FOCUSCODE:
//                bindFocusViewHolder(holder,position);
                focusesViewHolder = (FocusesViewHolder) holder;
                RecommendFocusAdapter focusAdapter = new RecommendFocusAdapter(focusviewList,context);
                focusesViewHolder.focuseViewPager.setAdapter(focusAdapter);
                focusesViewHolder.focuseViewPager.addOnPageChangeListener(this);

                if (focusviewList.size()>0){
                    focusAdapter.addData(focusviewList,focusList);
                    startTimer();
                }
                break;

        }

    }

    //bind more  ?
    private void bindMoreViewHolder(RecyclerView.ViewHolder holder, int position) {
        MoreViewHolder moreHolder = (MoreViewHolder) holder;
        moreHolder.layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MainActivity activity = (MainActivity) context;
                activity.getClassfiyFragment();
            }
        });
    }

    //bind尾部轮播
    private void bindFootViewHolder(RecyclerView.ViewHolder holder, int position) {

        Log.d("1620","bindfoot");

        FootViewHolder footHolder = (FootViewHolder) holder;

        FootRecommendAdapter footRecommendAdapter = new FootRecommendAdapter(context,footHolder.viewpager);
        footHolder.viewpager.setAdapter(footRecommendAdapter);



    }

    //绑定精品听单
    private void bindQualityViewHolder(RecyclerView.ViewHolder holder, int position) {
        QualityViewHolder qualityHolder = (QualityViewHolder) holder;
//        LinearLayoutManager manager = new LinearLayoutManager(context);
        GridLayoutManager manager = new GridLayoutManager(context,1);
        qualityHolder.recyclerview.setLayoutManager(manager);

        QualityRecommendAdapter qualityAdapter = new QualityRecommendAdapter(context);
        qualityHolder.recyclerview.setAdapter(qualityAdapter);

    }

    //bind小编推荐
    private void bindRecommendViewHolder(RecyclerView.ViewHolder holder, int position) {
        RecommendViewHolder recommendHolder = (RecommendViewHolder) holder;
        if (recommendList!=null) {
            GridLayoutManager manager = new GridLayoutManager(context,3);
            recommendHolder.recyclerView.setLayoutManager(manager);
            EditorRecommendAdapter recommendAdapter = new EditorRecommendAdapter(context,recommendList);
            recommendHolder.recyclerView.setAdapter(recommendAdapter);
        }

    }


    //猜你喜欢
    private void bindGuessViewHolder(RecyclerView.ViewHolder holder,int position) {
        GridLayoutManager manager = new GridLayoutManager(context,3);
        GuessViewHolder guessHolder = (GuessViewHolder) holder;
        guessHolder.recyclerView.setLayoutManager(manager);
        if (likeList.size()!=0) {
            RecommendGuessAdapter guessAdapter = new RecommendGuessAdapter(context,likeList,activity);
            guessHolder.recyclerView.setAdapter(guessAdapter);

        }

    }

    //绑定第二个pager  discovery
    private void bindDiscoveryViewHolder(RecyclerView.ViewHolder holder,int position) {
        DiscoveryViewHolder discoveryHolder = (DiscoveryViewHolder) holder;

        LinearLayoutManager manager = new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        discoveryHolder.recyclerView.setLayoutManager(manager);

        RecommendDiscoveryAdapter discoveryAdapter = new RecommendDiscoveryAdapter(discoveryList,context,activity);
        discoveryHolder.recyclerView.setAdapter(discoveryAdapter);

        if (discoveryList!=null) {
            discoveryAdapter.addDatas(discoveryList);
        }

    }

    //开启子线程，控制自动轮播 焦点图
    public void startTimer(){
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Message message = Message.obtain();
                message.what = 1;
                handler.sendEmptyMessage(message.what);
            }
        }, 0, 6000);
    }

    private void bindFocusViewHolder(RecyclerView.ViewHolder holder, int position) {



    }

    //绑定热门的holder
    private void bindHotViewHolder(RecyclerView.ViewHolder viewHolder,int position) {
        HotViewHolder holder = (HotViewHolder) viewHolder;
        GridLayoutManager manager = new GridLayoutManager(context,3);
        holder.recyclerView.setLayoutManager(manager);

        if (position<20){
            if (list.get(position).getTitle()!=null) {
                holder.mainTitle.setText(list.get(position).getTitle());
            }
            if (list.get(position).getList()!=null) {
                listenList=(list.get(position).getList());
            }
            LisstenRecommendApadter adapter = new LisstenRecommendApadter(context,listenList);
            holder.recyclerView.setAdapter(adapter);
        }


    }

    @Override
    public int getItemCount() {

        return list.size()==0?0:list.size()+6;
    }

    @Override
    public int getItemViewType(int position) {

        if (position==0){
            return FOCUSCODE;
        }
        if (position==1) {
            return DISCOVERYCODE;
        }
        if (position==2){
            return GUESSCODE;
        }if (position==3){
            return RECOMMENDCODE;
        }if (position==4){
            return QUALITYCODE;
        }
        if (position==5){
            return  HOTCODE;
        }
        if (position==25){
            return MORECODE;
        }
        if (position==26){
            return FOOTCODE;
        }
        else {
            return HOTCODE;
        }

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        focuscurposition = position;
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        switch (state) {
            case ViewPager.SCROLL_STATE_IDLE:
                if (focuscurposition==focusviewList.size()-1) {
                    focusesViewHolder.focuseViewPager.setCurrentItem(1,false);
                }
                if (focuscurposition==0) {
                    focusesViewHolder.focuseViewPager.setCurrentItem(focusviewList.size()-2,false);
                }
                if (timer==null) {
                    focuscurposition--;
                    startTimer();
                }
                break;
            case ViewPager.SCROLL_STATE_DRAGGING:
                if (timer!=null) {
                    timer.cancel();
                    timer=null;
                }
                break;

        }
    }

    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        this.recyclerView=null;
    }

    public  class FocusesViewHolder extends RecyclerView.ViewHolder{
        private ViewPager focuseViewPager;

        public FocusesViewHolder(View itemView) {
            super(itemView);
            focuseViewPager = (ViewPager) itemView.findViewById(R.id.recommend_focuspager);
        }
    }

    public class GuessViewHolder extends RecyclerView.ViewHolder{

        private RecyclerView recyclerView;

        public GuessViewHolder(View itemView) {
            super(itemView);
            recyclerView = (RecyclerView) itemView.findViewById(R.id.recommend_like_recyclerview);
        }
    }
    public class RecommendViewHolder extends RecyclerView.ViewHolder{
        private RecyclerView recyclerView;

        public RecommendViewHolder(View itemView) {
            super(itemView);
            recyclerView = (RecyclerView) itemView.findViewById(R.id.recommend_recommend_recyclerview);
        }
    }

    public class QualityViewHolder extends RecyclerView.ViewHolder{

        private final RecyclerView recyclerview;

        public QualityViewHolder(View itemView) {
            super(itemView);
            recyclerview = (RecyclerView) itemView.findViewById(R.id.recommend_quality_recyclerview);
        }
    }
    public class HotViewHolder extends RecyclerView.ViewHolder{
        private TextView mainTitle;

        private RecyclerView recyclerView;

        public HotViewHolder(View itemView) {
            super(itemView);
            mainTitle = (TextView) itemView.findViewById(R.id.recommend_maintitle);
            recyclerView = (RecyclerView) itemView.findViewById(R.id.recommendlistener_recyclerview);
        }

    }
    public class DiscoveryViewHolder extends RecyclerView.ViewHolder{
        private RecyclerView recyclerView;

        public DiscoveryViewHolder(View itemView) {
            super(itemView);
            recyclerView = (RecyclerView) itemView.findViewById(R.id.discovery_recyclerview);
        }
    }

    public class MoreViewHolder extends RecyclerView.ViewHolder{

        private  LinearLayout layout;

        public MoreViewHolder(View itemView) {
            super(itemView);
            layout = (LinearLayout) itemView.findViewById(R.id.recommend_more_mainlayout);
        }
    }
    public class FootViewHolder extends RecyclerView.ViewHolder{

        private final ViewPager viewpager;

        public FootViewHolder(View itemView) {
            super(itemView);
            viewpager = (ViewPager) itemView.findViewById(R.id.recommenn_foot_viewpager);
        }
    }
    private class HeaderViewHolder extends RecyclerView.ViewHolder{
        private TextView textView;
        public HeaderViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.recommend_head_title);
        }
    }


}
